package com.yq.day30.udp1.v3;

import com.yq.utils.NetUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import java.util.Scanner;

/**
 * @author 3307235355@qq.com
 * @description sender
 * @since 2022/06/30 16:55
 */

//发送端接收端相互发
public class Demo01 {
    public static void main(String[] args) throws IOException {
        //    创建发送端的对象
        DatagramSocket socket = new DatagramSocket(8888);

        //    scanner
        Scanner scanner = new Scanner(System.in);
        //      while
        while (true) {
            //     键盘接收 数据的封装
            String s = scanner.nextLine();

            //    send发送
            DatagramPacket sendPacket = NetUtils.getSendPacket(s, "127.0.0.1", 9999);
            socket.send(sendPacket);

            //    接收的逻辑
            DatagramPacket receivePacket = NetUtils.getReceivePacket();
            socket.receive(receivePacket);
            //    创建用于接收的数据报包
            //    receive接收
            //    解析
            String parse = NetUtils.parse(receivePacket);
            System.out.println(parse);
        }


    }
}
